package org.example.carsystem.service.impl;

import com.alibaba.excel.EasyExcel;
import org.example.carsystem.config.UsersDataListener;
import org.example.carsystem.domain.SysUser;
import org.example.carsystem.domain.dto.SysUserDTO;
import org.example.carsystem.mapper.UserMapper;
import org.example.carsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<SysUser> usersSelect(Long deptId, String userName, String phoneNumber, String status, Date createStartTime, Date createEndTime) {
        return userMapper.usersSelect(deptId,userName,phoneNumber,status,createStartTime,createEndTime);
    }

    @Override
    @Transactional
    public void usersAdd(SysUserDTO sysUserDTO) {
        sysUserDTO.setCreateBy("admin");
        userMapper.usersAdd(sysUserDTO);
        userMapper.usersPostAdd(sysUserDTO.getUserId(), sysUserDTO.getPostIds());
        userMapper.usersRolesAdd(sysUserDTO.getUserId(), sysUserDTO.getRoleIds());
    }

    @Override
    public SysUserDTO SelectById(Long userId) {
        SysUserDTO sysUserDTO = userMapper.SelectById(userId);
        if (null!= sysUserDTO.getRoleIdAll()){
            String[] stringRoleIds = sysUserDTO.getRoleIdAll().split(",");
            Long[] roleIds = Arrays.stream(stringRoleIds).map(Long::valueOf).toArray(Long[]::new);
            sysUserDTO.setRoleIds(roleIds);
        }
        if (null!= sysUserDTO.getPostIdAll()){
            String[] stringPostIds = sysUserDTO.getPostIdAll().split(",");
            Long[] postIds = Arrays.stream(stringPostIds).map(Long::valueOf).toArray(Long[]::new);
            sysUserDTO.setPostIds(postIds);
        }
        return sysUserDTO;
    }

    @Override
    @Transactional
    public void DeleteUser(Long[] userId) {
        userMapper.DeleteUser(userId);
        userMapper.DeleteUserRoles(userId);
        userMapper.DeleteUserPost(userId);
    }

    @Override
    public void updateUser(SysUserDTO sysUserDTO) {
        sysUserDTO.setUpdateBy("admin");
        userMapper.updateUser(sysUserDTO);
    }

    @Override
    public void updateUserStatus(Long userId, String status) {
        String updateBy = "admin";
        userMapper.updateUserStatus(updateBy,userId,status);
    }

    @Override
    public void exportUsers(Long deptId, String userName, String phoneNumber, String status, Date createStartTime, Date createEndTime) {
        List<SysUser> sysUserList = userMapper.usersSelect(deptId, userName, phoneNumber, status, createStartTime, createEndTime);
    }

    @Override
    public void importUsers(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), SysUser.class,new UsersDataListener(userMapper,redisTemplate)).sheet().doRead();
    }
}
